package dianmobile.eistarandroid.network.request;

import android.content.Context;

import com.android.volley.Response;

import org.xmlpull.v1.XmlPullParser;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import dianmobile.eistarandroid.beans.MyArticleEntity;
import dianmobile.eistarandroid.utils.ConstantsData;
import dianmobile.eistarandroid.utils.HandleString;

/**
 * Created by Administrator on 2015/8/15.
 */
public class MyArticleRequest extends ByhhAndroidRequest {

    private int startNum;
    private int lastStartNum;

    public static void execute(Context context, Map<String, String> params, RequestCallback callback) {
        executeSuper(context, RequestType.MYPOSTS, params, callback);
    }
    public MyArticleRequest(int method, String url, Map<String, String> params,
                        RequestCallback callback, Response.ErrorListener listener) {
        super(method, url, params, callback, listener);
        lastStartNum = Integer.parseInt(params.get("lastStartNum"));
    }

    @Override
    protected void parseXmlToMap(XmlPullParser parser, Map<String, Object> map) {
        final String TAG_TITLE_LIST = "titlelist";
        final String TAG_TITLE = "title";
        final String TAG_BOARD = "board";
        final String TAG_DATE = "date";
        final String TAG_FILENAME = "filename";
        final String TAG_NAME = "name";
        final String TAG_NUM = "num";

        List<MyArticleEntity> data = new ArrayList<>();
        MyArticleEntity item = null;

        try {
            int event = parser.getEventType();
            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String tagName = parser.getName();

                        if (tagName.equals(TAG_TITLE_LIST)) {
                            startNum = Integer.parseInt(parser.getAttributeValue(null, "start"));

                        } else if (tagName.equals(TAG_TITLE)) {
                            item = new MyArticleEntity();
                        } else if (tagName.equals(TAG_BOARD) && item != null) {
                            item.setBoardName(parser.nextText());
                        } else if (tagName.equals(TAG_DATE) && item != null) {
                            item.setPostTime(HandleString.getTimeDifference(parser.nextText()));
                        }else if (tagName.equals(TAG_NAME) && item != null) {
                            item.setArticleTitle(parser.nextText());
                        } else if (TAG_FILENAME.equals(tagName) && item != null) {
                            item.setFileName(parser.nextText());
                        } else if (TAG_NUM.equals(tagName)) {
                            int num = Integer.parseInt(parser.nextText());
                            if (num >= lastStartNum) {
                                item = null;    //如果该item已经在上一列表出现，则不添加到此列表
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTag = parser.getName();
                        if (endTag.equals(TAG_TITLE) && item != null) {
                            data.add(item);
                            item = null;
                        }

                        break;
                    default:
                        break;
                }

                event = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Collections.reverse(data);  //返回的是按时间从以前到现在排序，将其翻转
        map.put("result_data", data);
        map.put(ConstantsData.EXTRA_START_NUM, startNum);
    }
}
